package tcln.gxtx.client.widget.dialog;

import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.util.Size;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Element;

/**
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class ErrorMessageBox extends Dialog {
    private static final Size DEFAULT_SIZE = new Size(600, 450);

    private TextField<String> errorMessageField;
    private TextField<String> exceptionClassNameField;
    private TextArea stackTraceField;

    /**
     * @param title
     * @param errorMessage
     * @param exceptionClassName
     * @param exceptionStacktrace
     * @param callback
     */
    public ErrorMessageBox(String title, String errorMessage, String exceptionClassName, String exceptionStacktrace,
            Listener<MessageBoxEvent> callback) {
        super();
        setHeading(title);
        setBodyBorder(false);
        setHideOnButtonClick(true);
        setSize(DEFAULT_SIZE.width, DEFAULT_SIZE.height);
        setLayout(new FitLayout());

        LayoutContainer detailPanel = initializeDetailPanel(title, errorMessage, exceptionClassName,
                exceptionStacktrace);
        this.add(detailPanel);
        this.setFocusWidget(errorMessageField);

        if (callback != null) {
            addListener(Events.Hide, callback);
        }
    }

    private LayoutContainer initializeDetailPanel(String title, String errorMessage, String exceptionClassName,
            String exceptionStacktrace) {
        LayoutContainer detailPanel = new LayoutContainer();

        FormLayout layout = new FormLayout();
        detailPanel.setLayout(layout);

        this.errorMessageField = new TextField<String>();
        this.errorMessageField.setValue(errorMessage);
        this.errorMessageField.setReadOnly(true);
        this.errorMessageField.setFieldLabel("Error");
        detailPanel.add(this.errorMessageField, new FormData("100%"));

        this.exceptionClassNameField = new TextField<String>();
        this.exceptionClassNameField.setValue(exceptionClassName);
        this.exceptionClassNameField.setReadOnly(true);
        this.exceptionClassNameField.setFieldLabel("Exception class");
        detailPanel.add(this.exceptionClassNameField, new FormData("100%"));

        this.stackTraceField = new TextArea();
        this.stackTraceField.setValue(exceptionStacktrace);
        this.stackTraceField.setReadOnly(true);
        this.stackTraceField.setFieldLabel("Stack trace");
        detailPanel.add(this.stackTraceField, new FormData("100% -56"));

        return detailPanel;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);
        
        this.stackTraceField.setInputStyleAttribute("fontFamily", "Courier");
        this.stackTraceField.setInputStyleAttribute("fontSize", "13px");
        this.stackTraceField.setInputStyleAttribute("fontColor", "red");
        this.stackTraceField.setInputStyleAttribute("wrap", "off");
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void onUnload() {
        super.onUnload();

        this.errorMessageField.clear();
        this.exceptionClassNameField.clear();
        this.stackTraceField.clear();
    }
}
